var reg1 = /0*([1-9]\d*|0\.\d+)/;
var reg2 = /(?:\.0*|(\.\d+?)0+)$/;
function showLoading(content){
	uni.showLoading({
		title : content,
		mask:true
	});
}
function hideLoading(){
	uni.hideLoading();
}
function showToastSuc(content){
	uni.showToast({
		title: content,
		icon: 'success',
		duration: 1800
	})
}
function showToast(content,duration){
	uni.showToast({
		title: content,
		icon: 'none',
		duration: duration == undefined ? 1800 : duration
	})
}
function setClipborarData(content){
	uni.setClipboardData({
		data:content,
		success:()=>{
			uni.showToast({
				title: '复制成功',
				icon: 'success',
				duration: 1800
			})
		}
	})
}
function navigateTo(page){
	uni.navigateTo({
		url: page
	})
}
function comNavigateToLogin(opt,canRefreshFlag){
	uni.navigateTo({
		url:'/pages/login/login?loadOpt=' + opt + '&canRefreshFlag=' + canRefreshFlag
	})
}
function redirectTo(page){
	uni.redirectTo({
		url: page
	})
}
function trim(str){
	return str.replace(/(^\s*)|(\s*$)/g, ""); 
}
function patternNum(str){
	if(str != ''){
		str = parseFloat(str.replace(reg1, '$1').replace(reg2, '$1')).toFixed(2);
	}else{
		str = 0;
	}
	return str;
}
function formatLocation(longitude, latitude) {
	if (typeof longitude === 'string' && typeof latitude === 'string') {
		longitude = parseFloat(longitude)
		latitude = parseFloat(latitude)
	}

	longitude = longitude.toFixed(2)
	latitude = latitude.toFixed(2)

	return {
		longitude: longitude.toString().split('.'),
		latitude: latitude.toString().split('.')
	}
}
function isEmojiCharacter(substring){
	if(substring){
		for ( var i = 0; i < substring.length; i++) {
			var hs = substring.charCodeAt(i);
			if (0xd800 <= hs && hs <= 0xdbff) {
				if (substring.length > 1) {
					var ls = substring.charCodeAt(i + 1);
					var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;
					if (0x1d000 <= uc && uc <= 0x1f77f) {
						return true;
					}
				}
			} else if (substring.length > 1) {
				var ls = substring.charCodeAt(i + 1);
				if (ls == 0x20e3) {
					return true;
				}
			} else {
				if (0x2100 <= hs && hs <= 0x27ff) {
					return true;
				} else if (0x2B05 <= hs && hs <= 0x2b07) {
					return true;
				} else if (0x2934 <= hs && hs <= 0x2935) {
					return true;
				} else if (0x3297 <= hs && hs <= 0x3299) {
					return true;
				} else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030
					|| hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b
					|| hs == 0x2b50) {
					return true;
				}
			}
		}
	}
}
function Str2Bytes(str){
	var pos = 0;
	var len = str.length;
	if(len %2 != 0)
	{
	   return null; 
	}
	len /= 2;
	var hexA = new Array();
	for(var i=0; i<len; i++)
	{
	   var s = str.substr(pos, 2);
	   var v = parseInt(s, 16);
	   if(v>=128) v = v | 0xFFFFFF00;
	   hexA.push(v);
	   pos += 2;
	}
	return hexA;
}
function openMapNaviagtion(lon,lat,cpyName){
	//打开第三方地图
	var url = "",mapApp = '';
	if (plus.os.name == "Android") {//判断是安卓端
		plus.nativeUI.actionSheet({//选择菜单
			title: "",
			cancel: "取消",
			buttons: [{title: "高德地图"},{title: "百度地图"},{title: "腾讯地图"}]
		}, function(e) {
			switch (e.index) {
				//下面是拼接url,不同系统以及不同地图都有不同的拼接字段
				case 1:
					mapApp = '高德地图';
					url = `androidamap://viewMap?sourceApplication=appname&poiname=${cpyName}&lat=${lat}&lon=${lon}&dev=0`;
					break;
				case 2:
					mapApp = '百度地图';
					url = `baidumap://map/marker?location=${lat},${lon}&title=${cpyName}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
					break;
				case 3:
					//注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
					mapApp = '腾讯地图';
					url = `qqmap://map/geocoder?coord=${lat},${lon}&referer=xxx`;
					break;
				default:
					break;
			}
			if (url != "") {
				url = encodeURI(url);
				//plus.runtime.openURL(url,function(e){})调起手机APP应用
				plus.runtime.openURL(url, function(e) {
					uni.showToast({
						icon:'none',
						title:'本机未安装' + mapApp + '应用'
					})
				});
			}
		})
	}
}
function appendZero(obj){
	if (obj < 10) {
	  return '0' + obj 
	} else {
	  return obj
	} 
}
function getNowTimeDay(){
	var date = new Date();
	return date.getFullYear() + '-' + appendZero((date.getMonth()+1)) + '-' + appendZero(date.getDate());
}
function getNowTimeMonth(){
	var date = new Date();
	return date.getFullYear() + '-' + appendZero((date.getMonth()+1));
}
function commonMultiSelect(obj,newObj,index,name){
	if(obj[index].selFlag){
		obj[index].selFlag = false;
		for(var i=0;i<newObj.length;i++){
			if(name == newObj[i]){
				newObj.splice(i,1);
			}
		}
	}else if(obj[index].selFlag == false) {
		obj[index].selFlag = true;
		newObj.push(obj[index].name)
	}
	//console.log(newObj)
}
function switchWordsToNum(str){
	let tmpIndex = -1;
	switch (str) {
		case 'A':
			tmpIndex = 0;
			break;
		case 'B':
			tmpIndex = 1;
			break;
		case 'C':
			tmpIndex = 2;
			break;
		case 'D':
			tmpIndex = 3;
			break;
		case 'E':
			tmpIndex = 4;
			break;
		case 'F':
			tmpIndex = 5;
			break;
		case 'G':
			tmpIndex = 6;
			break;
	}
	return tmpIndex;
}
function dateToWeek(time){
	const weekNum = new Date(time).getDay();
	let week = '';
	switch (weekNum) {
	    case 0:
	      week = "星期日";
	      break;
	    case 1:
	      week = "星期一";
	      break;
	    case 2:
	      week = "星期二";
	      break;
	    case 3:
	      week = "星期三";
	      break;
	    case 4:
	      week = "星期四";
	      break;
	    case 5:
	      week = "星期五";
	      break;
	    case 6:
	      week = "星期六";
	      break;
	  }
	  return week;
}
function formatWeek(str){
	let newWeek = '';
	switch(str){
		case '星期一':
			newWeek = '周一';
			break;
		case '星期二':
			newWeek = '周二';
			break;
		case '星期三':
			newWeek = '周三';
			break;
		case '星期四':
			newWeek = '周四';
			break;
		case '星期五':
			newWeek = '周五';
			break;
		case '星期六':
			newWeek = '周六';
			break;
		case '星期日':
			newWeek = '周日';
			break;
	}
	return newWeek;
}
function calculateDiffTime(startTime,endTime){
	let runTime = parseInt((endTime - startTime) / 1000);
	let year = Math.floor(runTime / 86400 / 365);
	runTime = runTime % (86400 * 365);
	let month = Math.floor(runTime / 86400 / 30);
	runTime = runTime % (86400 * 30);
	let day = Math.floor(runTime / 86400);
	runTime = runTime % 86400;
	let hour = Math.floor(runTime / 3600);
	runTime = runTime % 3600;
	let minute = Math.floor(runTime / 60);
	runTime = runTime % 60;
	let second = runTime;
	let result = {
		year,
		month,
		day,
		hour,
		minute,
		second
	};
	//console.log(`相差${year}年${month}月${day}天${hour}小时${minute}分${second}秒`);
	console.log(second + '秒')
	return result;
}
function calculateDiffTime_1(autoTime,opt){
	//opt->returnStr returnObj
	var date1 = (Date.parse(new Date()))/1000;//计算当前时间戳 
	var date2 = (Date.parse(new Date(autoTime)))/1000;; //自动收货的时间戳 （字符串转时间戳）
	var date3 =  (date2- date1)*1000; //时间差的毫秒数
	//计算出相差天数
	var days = Math.floor(date3 / (24 * 3600 * 1000));
	//计算出小时数
	var leave1 = date3 % (24 * 3600 * 1000); //计算天数后剩余的毫秒数
	var hours = Math.floor(leave1 / (3600 * 1000));
	//计算相差分钟数
	var leave2 = leave1 % (3600 * 1000); //计算小时数后剩余的毫秒数
	var minutes = Math.floor(leave2 / (60 * 1000));
	//计算相差秒数
	var leave3 = leave2 % (60 * 1000); //计算分钟数后剩余的毫秒数
	var seconds = Math.round(leave3 / 1000);
	//console.log(days + "天 " + hours + "小时 ")
	//console.log(days + "天 " + hours + "小时 " + minutes + " 分钟" + seconds + " 秒");
	//return  days + "天" + hours + "小时"
	if(opt == 'returnStr'){
		var str = '';
		if(days != 0){
			str = days + "天" + hours + "小时" + minutes + "分钟" + seconds + "秒"
		}else{
			if(hours != 0){
				str = hours + "小时" + minutes + "分钟" + seconds + "秒"
			}else{
				if(minutes != 0){
					str = minutes + "分钟" + seconds + "秒"
				}else{
					str = seconds + "秒"
				}
			}
		}
		return str;
	}else{
		var fieldObj = { days:days,hours:hours,minutes:minutes,seconds };
		return fieldObj;
	}
}
function replaceToBr(str){
	return str.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, ' ');
}
export default {
	showLoading,
	hideLoading,
	showToastSuc,
	showToast,
	navigateTo,
	redirectTo,
	trim,
	isEmojiCharacter,
	Str2Bytes,
	patternNum,
	formatLocation,
	openMapNaviagtion,
	setClipborarData,
	comNavigateToLogin,
	commonMultiSelect,
	dateToWeek,
	switchWordsToNum,
	formatWeek,
	getNowTimeDay,
	getNowTimeMonth,
	calculateDiffTime,
	calculateDiffTime_1,
	replaceToBr
}
